home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 17 / CU Amiga Magazine's Super CD-ROM 17 (1997)(EMAP Images)(GB)[!][issue 1997-12].iso / CUCD / Programming / DiceSource / lib / fd / cache.c next >
C/C++ Source or Header  |  1997-09-09  |  2KB  |  102 lines

  1.  
  2. /*
  3.  *  CACHE.C
  4.  *
  5.  *  Implement DICECACHE.LIBRARY access
  6.  *
  7.  *    (c)Copyright 1992-1997 Obvious Implementations Corp.  Redistribution and
  8.  *    use is allowed under the terms of the DICE-LICENSE FILE,
  9.  *    DICE-LICENSE.TXT.
  10.  *
  11.  */
  12.  
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include <ioctl.h>
  16. #include <fcntl.h>
  17. #include <errno.h>
  18. #include <lib/misc.h>
  19. #include <clib/dicecache_protos.h>
  20. #include <clib/exec_protos.h>
  21.  
  22. typedef struct CacheFh {
  23.     CacheNode    *cfh_Cn;
  24.     long    cfh_Size;
  25.     long    cfh_Pos;
  26. } CacheFh;
  27.  
  28. void
  29. _MakeCacheFD(d, cn, size)
  30. _IOFDS *d;
  31. void *cn;
  32. long size;
  33. {
  34.     CacheFh *cfh = malloc(sizeof(CacheFh));
  35.  
  36.     if (cfh)
  37.     {
  38.         d->fd_Fh = (long)cfh;
  39.         d->fd_Exec = _CacheFDIoctl;
  40.         cfh->cfh_Cn = cn;
  41.         cfh->cfh_Size = size;
  42.         cfh->cfh_Pos = 0;
  43.     }
  44. }
  45.  
  46. _CacheFDIoctl(fh, cmd, arg1, arg2)
  47. long fh;
  48. int cmd;
  49. void *arg1;
  50. void *arg2;
  51. {
  52.     CacheFh *cfh = (CacheFh *)fh;
  53.     long r = 0;
  54.  
  55.     /*
  56.      *    most common operation
  57.      */
  58.  
  59.     if (cmd == IOC_READ) {
  60.     void *ptr;
  61.     long n;
  62.  
  63.     if (ptr = DiceCacheSeek(cfh->cfh_Cn, cfh->cfh_Pos, &n)) {
  64.         if (n > (long)arg2)
  65.         n = (long)arg2;
  66.         CopyMem(ptr, arg1, n);
  67.         cfh->cfh_Pos += n;
  68.     } else {
  69.         n = -1;
  70.     }
  71.     return(n);
  72.     }
  73.  
  74.     switch(cmd) {
  75.     case IOC_CLOSE:
  76.     DiceCacheClose(cfh->cfh_Cn);
  77.     free(cfh);
  78.     case IOC_SEEK:
  79.     r = (long)arg1;
  80.  
  81.     switch((long)arg2) {
  82.     case 1:
  83.         r += cfh->cfh_Pos;
  84.         break;
  85.     case 2:
  86.         r = cfh->cfh_Size - r;
  87.         break;
  88.     }
  89.     if (r < 0 || r > cfh->cfh_Size)
  90.         r = -1;
  91.     else
  92.         cfh->cfh_Pos = r;
  93.     break;
  94.     default:
  95.     r = -1;
  96.     errno = EINVAL;
  97.     break;
  98.     }
  99.     return(r);
  100. }
  101.  
  102.